package cn.sc.allinpay.allfavour.business.check;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.pai.common.listener.OnPromptListener;
import cn.pai.mvp.callback.MpCallBack;
import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.checkdetail.CheckDetailActivity;
import cn.sc.allinpay.allfavour.business.checkscan.CheckScanActivity;
import cn.sc.allinpay.allfavour.business.common.data.CheckCouponData;
import cn.sc.allinpay.allfavour.business.common.utils.SpUtils;

/**
 * 验券 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class CheckPresenter extends PaiPresenter<CheckContract.View> implements CheckContract.Presenter {

    public static final int REQ_CHECKDETAIL = 0x01;//验券详情

    private CheckModel model = new CheckModel();
    private String code;//扫码code

    @Override
    protected Intervenor createIntervenor() {
        return new Intervenor() {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            }

            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == REQ_CHECKDETAIL) {
                    view.finish();
                } else {
                    // 扫码返回后,获取解析结果
                    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                    if (result != null) {
                        code = result.getContents();
                        if (!TextUtils.isEmpty(code)) {
                            view.toast("扫描" + code);
                            reqQueryCoupon();
                        } else {
                            view.toast("取消扫描");
                            view.finish();
                        }
                    } else {
                        super.onActivityResult(requestCode, resultCode, data);
                    }
                }
            }
        };
    }

    @Override
    public void start() {

        new IntentIntegrator(view.getActivity()).setOrientationLocked(false).
                setCaptureActivity(CheckScanActivity.class).initiateScan();

        //设置的备注信息
        String remarkstr = SpUtils.get(view.getContext(), Constant.SPNAME, Constant.SpKey.CheckRemarks, Constant.DEFAULT_REMARK);
        List<String> remarks = new ArrayList<>();
        remarks.add("点击选择验券备注信息");
        if (!TextUtils.isEmpty(remarkstr)) {
            String[] remarkArray = remarkstr.split("!");
            remarks.addAll(Arrays.asList(remarkArray));
        }
        view.setRemarkAdapter(remarks);
    }

    @Override
    public void reqQueryCoupon() {
        if (code.length() < 8) {
            view.toast("请扫描有效的二维码");
            return;
        }
        view.loading();
        model.reqQueryCouponByNo(code, new MpCallBack<List<CheckCouponData>, String>() {
            @Override
            public void onSuccess(List<CheckCouponData> resp) {
                view.loaded();
                view.couponsAdapterChange(resp);
            }

            @Override
            public void onFailure(String response, String message) {
                view.loaded();
                view.prompt(false, message + "，是否重新扫码?", new OnPromptListener("确认", "取消") {
                    @Override
                    public void onPositive(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        new IntentIntegrator(view.getActivity()).setOrientationLocked(false).
                                setCaptureActivity(CheckScanActivity.class).initiateScan();
                    }

                    @Override
                    public void onNegative(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        view.finish();
                    }
                });

            }
        });
    }

    @Override
    public void startCheckDetailActivity() {
        ArrayList<CheckCouponData> datas = (ArrayList<CheckCouponData>) view.getCouponsAdapterDatas();
        ArrayList<CheckCouponData> coupons = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            CheckCouponData data = datas.get(i);
            if (data.isUse()) {
                data.setRemark(view.getSelectRemark());
                coupons.add(data);
            }
        }
        if (coupons.isEmpty()) {
            view.prompt("请设置验券金额或者数量");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(Constant.BundleKey.CHECKCOUPON, coupons);
        view.toActivityForResult(CheckDetailActivity.class, bundle, REQ_CHECKDETAIL);
    }
}
